local addonName, IWonew = ...

EmailSenderClass = {}
EmailSenderClass.__index = EmailSenderClass


function EmailSenderClass:new()
  local instance = setmetatable({}, EmailSenderClass)  -- Create a new object, and set its metatable to Person
  return instance
end

function EmailSenderClass:SendCraftResultToTargetRealmRole()
  
  local realmName = GetRealmName()
  local currentRole = UnitName("player")
  
  local roleName = "@dddx小拉"
  if realmName == "死亡之翼" then
    roleName = "小兔丶罗西"
  end
  if realmName == "白银之手" then
    roleName = "小狼温蒂丶"
  end
  if roleName == currentRole or "@dddx小拉" == roleName then
    print("不能给自己发送邮件")
    return
  end
  -- 223692 晶脉辉煌3星
  -- 222428 镔爪合金3星
  -- 223790 诅咒急速3星
  local itemIDs = {222428, 223692, 223790}
  local itemSlotList = {}
  local ignoreItems = {
    "附魔羊皮纸", "晶脉粉末", "专注奇思瓶剂", "铋棒", "复活节兔宝宝", "热流铁砧"
  }
  local mustItems = {"晶脉辉煌", "镔爪合金", "诅咒急速"}
  local bagItems = IWonew.GetAllBagItems()
  local mustItemSlots = {}
  local remainItemSlots = {}
  for i = 1, #bagItems do
    local itemName = bagItems[i].itemName
    -- 宠物符咒
    if bagItems[i].itemID == 163036 then
      table.insert(mustItemSlots, bagItems[i])
    end
    if (not bagItems[i].isBound) and (not IWonew.contains(ignoreItems, itemName)) then
      if IWonew.contains(mustItems, itemName) then
        table.insert(mustItemSlots, bagItems[i])
      else
        table.insert(remainItemSlots, bagItems[i])
      end
      -- C_Container.UseContainerItem(bagItems[i].bagID, bagItems[i].slotID, "unitToken", Enum.BankType.Account)
    end
  end
  local resultSlots = {}
  if #mustItemSlots + #remainItemSlots > 12 then
    resultSlots = IWonew.concat(mustItemSlots, IWonew.slice(remainItemSlots, 1, 12 - #mustItemSlots))
  else
    resultSlots = IWonew.concat(mustItemSlots, remainItemSlots)
  end
  if #resultSlots <= 0 then
    print("没有可发送的物品")
    return
  end
  local bagMoney = IWonew.GetBagMoney()
  local sendGold = 0
  if bagMoney.gold > 100 then
    sendGold = (bagMoney.gold - 100) * 10000
  end
  SetSendMailMoney(sendGold)
  -- for i = 1, #itemIDs do
  --   local itemSlot = IWonew.GetItemSlot(itemIDs[i])
  --   if itemSlot then
  --     table.insert(itemSlotList, itemSlot)
  --   end
  -- end
  self:SendItemToRole(roleName, resultSlots, function()
    print("发送完成")
  end)
end

function EmailSenderClass:SendItemToRole(roleName, itemSlotList, callback)
  if #itemSlotList == 0 then
    print("没有找到物品")
    return
   end
  local i = 1
  local ticker
  ticker = C_Timer.NewTicker(0.3, function()
    -- print("i--", i, "ss---", itemSlotList[i].bagID, itemSlotList[i].slotID)
    C_Container.PickupContainerItem(itemSlotList[i].bagID, itemSlotList[i].slotID)
    ClickSendMailItemButton(i)
    i = i + 1
    if i > #itemSlotList then
      ticker:Cancel()
      SendMail(roleName, "2", "")
      print("send to: ", roleName)
      C_Timer.After(1, function()
        if callback then
          callback()
        end
      end)
    end
  end)
end

function EmailSenderClass:SendSalvageCard(itemID, startIdx, endIdx)
  if itemID ~= nil then
    AccountRoleList = AccountRoleList or {}
    self.SendSalvageCard_temp = {
      roleList = IWonew.slice(AccountRoleList, startIdx, endIdx),
      itemID = itemID,
    }
  end
  local curRole = self.SendSalvageCard_temp.roleList[1]
  if curRole == nil then
    print("Send Done!!")
    return
  end
  print("curRole", curRole.name)
  local cardSlots = IWonew.FilterItemSlotByIDAndQuality(self.SendSalvageCard_temp.itemID, 1)
  self:SendItemToRole(curRole.name, IWonew.slice(cardSlots, 1, 6), function()
    table.remove(self.SendSalvageCard_temp.roleList, 1)
    self:SendSalvageCard()
  end)
end

local EmailSenderClass_instance = nil

function CreateEmailSenderInstance()
  if EmailSenderClass_instance == nil then
    EmailSenderClass_instance = EmailSenderClass:new()
  end
  return EmailSenderClass_instance
end
-- /dump DebugEmailSender(222671, 1, 11)
-- /dump DebugEmailSender(222671, 12, 30)
-- /dump DebugEmailSender(222671, 31, 45)
-- /dump DebugEmailSender(222671, 46, 56)


-- /dump DebugEmailSender(222653, 1, 11)
-- /dump DebugEmailSender(222653, 12, 30)
-- /dump DebugEmailSender(222653, 31, 45)
-- /dump DebugEmailSender(222653, 46, 56)

-- /dump DebugEmailSender(222653, 12, 13)

function DebugEmailSender(itemID, startIdx, endIdx)
  -- 盎溢之
  -- 共生之
  -- 扬升之
  -- 光辉之
  CreateEmailSenderInstance():SendSalvageCard(itemID, startIdx, endIdx)
end

function SendCraftMailItem()
  CreateEmailSenderInstance():SendCraftResultToTargetRealmRole()
end